package com.csair.platform.cit.service.util;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.poi.ss.formula.functions.T;

import java.lang.reflect.Type;
import java.util.*;

public class JsonHelper {

	public static<T> List<T> from(List<?> list, Type clz) {
		if (list == null) {
			return null;
		}
		Gson gson = new Gson();
		String json = gson.toJson(list);
		List<T> rList = gson.fromJson(json, clz);
		return rList;
	}
	public static<T> Object from(T t,Type clz){
		if(Objects.isNull(t)){
			return null;
		}
		Gson gson = new Gson();
		String json = gson.toJson(t);
		T obj = gson.fromJson(json, clz);
		return obj;
	}

	public static Map<String, Object> fromMap(Object t){
		if(Objects.isNull(t)){
			throw new NullPointerException();
		}
		Map<String, Object> map = new HashMap<>();
		JSONObject jsonObj = JSONObject.parseObject(JSONObject.toJSONString(t));
		Set<String> keys = jsonObj.keySet();
		for (String key:keys) {
			map.put(key,jsonObj.get(key));
		}
		return map;
	}
}
